global dir "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"


use "$dir\Data\council_cand_level_persist_rep.dta",clear




gen treat=0
replace treat=1 if rv2>0&rv2~=.

gen treat_rv2=treat*rv2
	

	texdoc close 
	cap erase "$dir/Tables/TableH1.tex"
	texdoc init "$dir/Tables/TableH1.tex", force

	tex \begin{table}[tbph]
	tex \caption{Effect of donating to an election winner on future donations (local councils)}\label{tab:donations_council}
	tex \centering
	tex \begin{tabular}{l c c c} \hline
	tex Outcome:&  Any race  & Council & Runs again \\
	tex & (1) & (2) & (3) \\ \hline
	tex & & & \\
	
	*Model 1
	foreach x in donate_any15 b5 runs_concejo{
	
		*Summary statistics for the mean
		quietly sum `x' if  rv2!=.
			local mean_`x' : di %5.3f r(mean)
			local sd_`x' : di %5.3f r(sd) 
		
		*Regressions
		rdrobust `x' rv2 ,  p(1) vce(cluster muni_code)
		
		*Local's for the table
		local bw_`x' : di %5.2f `e(h_l)'
		local ser_`x' = round(`e(se_tau_rb)',0.001)
		local Neff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local N_`x' = `e(N)'
		local poly_`x' = `e(p)'
		local beta1_`x' : di %5.3f `e(tau_cl)'
		local beta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local ser1_`x' : di %5.3f `e(ci_l_rb)'
			local ser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local em1_`x' = (`beta1_`x''/`mean_`x'')*100 
			local em1_`x' : di %5.2f `em1_`x''
			
		*P-values
		local pval2_`x' : di %5.3f `e(pv_rb)'
		scalar pval2_`x' = e(pv_rb)
		
		
		regress `x' treat treat_rv2 rv2 , vce(cluster muni_code)

		local N_`x' : di %5.0f e(N)
		local R2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local b1_`x' : di %5.3f b[1,1]
		local se1_`x' : di %5.3f sqrt(v[1,1])
		local p_v_`x' :di %5.3f res[4,1]
		local uci_`x': di %5.3f res[6,1]
		local lci_`x': di %5.3f res[5,1]
		
	}	
	
	*Continue table
	tex \multicolumn{3}{l}{Local linear}&\\

	tex Electoral victory & `beta1_donate_any15' & `beta1_b5' & `beta1_runs_concejo' \\
	tex \ \ \ \ Robust p-value & `pval2_donate_any15' & `pval2_b5' & `pval2_runs_concejo' \\
	tex \ \ \ \ CI 95\%  & [`ser1_donate_any15',`ser2_donate_any15'] & [`ser1_b5',`ser2_b5'] & [`ser1_runs_concejo',`ser2_runs_concejo']\\
	tex & & & \\
	
	tex \multicolumn{3}{l}{Parametric (linear)}&\\

    tex Electoral victory & `b1_donate_any15' & `b1_b5' & `b1_runs_concejo' \\
	tex \ \ \ \ p-value & `p_v_donate_any15' & `p_v_b5' & `p_v_runs_concejo' \\
	tex \ \ \ \ CI 95\%  & [`lci_donate_any15',`uci_donate_any15'] & [`lci_b5',`uci_b5'] & [`lci_runs_concejo',`uci_runs_concejo'] \\
	tex & & & \\
	
	tex Observations & `N_donate_any15' & `N_b5' & `N_runs_concejo' \\
	tex Bandwidth obs. & `Neff_donate_any15' & `Neff_b5' & `Neff_runs_concejo' \\
	tex Mean & `mean_donate_any15' & `mean_b5' & `mean_runs_concejo' \\
	*tex Effect Mean(\%) & `em1_donate_15any' & `em1_b5' & `em1_b2b' \\
	tex Bandwidth & `bw_donate_any15' & `bw_b5' & `bw_runs_concejo' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{
	tex Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. 95\% robust confidence intervals and robust p-values with clustering at the municipality level are computed following \cite{calonico_robust_2014}. Parametric linear model specification includes interaction of the treatment with running variable and running variable. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 
